﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
namespace Chenxin.Lib.House.DAL
{
    /// <summary>
    /// 数据访问类Customer_ViewHouseInfo。
    /// </summary>
    public class Customer_ViewHouseInfo
    {
        public Customer_ViewHouseInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RecordId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Customer_ViewHouseInfo where RecordId=@RecordId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.House.Entity.Customer_ViewHouseInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Customer_ViewHouseInfo(");
            strSql.Append("RecordId,ReportCode,CustomerId,CustomerName,Description,Reportor,ReportorId,ReportDepart,DepartId,OperatorId,Operator,OprDepartId,OprDepart,CityId,CompanyId,AddTime,Status,houseId,houseAddress,ViewTime)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@ReportCode,@CustomerId,@CustomerName,@Description,@Reportor,@ReportorId,@ReportDepart,@DepartId,@OperatorId,@Operator,@OprDepartId,@OprDepart,@CityId,@CompanyId,@AddTime,@Status,@houseId,@houseAddress,@ViewTime)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "ReportCode", DbType.AnsiString, model.ReportCode);
            db.AddInParameter(dbCommand, "CustomerId", DbType.Guid, model.CustomerId);
            db.AddInParameter(dbCommand, "CustomerName", DbType.AnsiString, model.CustomerName);
            db.AddInParameter(dbCommand, "Description", DbType.AnsiString, model.Description);
            db.AddInParameter(dbCommand, "Reportor", DbType.AnsiString, model.Reportor);
            db.AddInParameter(dbCommand, "ReportorId", DbType.Guid, model.ReportorId);
            db.AddInParameter(dbCommand, "ReportDepart", DbType.AnsiString, model.ReportDepart);
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "OperatorId", DbType.Guid, model.OperatorId);
            db.AddInParameter(dbCommand, "Operator", DbType.AnsiString, model.Operator);
            db.AddInParameter(dbCommand, "OprDepartId", DbType.Guid, model.OprDepartId);
            db.AddInParameter(dbCommand, "OprDepart", DbType.AnsiString, model.OprDepart);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, model.Status);
            db.AddInParameter(dbCommand, "houseId", DbType.Guid, model.houseId);
            db.AddInParameter(dbCommand, "houseAddress", DbType.AnsiString, model.houseAddress);
            db.AddInParameter(dbCommand, "ViewTime", DbType.DateTime, model.ViewTime);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.House.Entity.Customer_ViewHouseInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Customer_ViewHouseInfo set ");
            strSql.Append("ReportCode=@ReportCode,");
            strSql.Append("CustomerId=@CustomerId,");
            strSql.Append("CustomerName=@CustomerName,");
            strSql.Append("Description=@Description,");
            strSql.Append("Reportor=@Reportor,");
            strSql.Append("ReportorId=@ReportorId,");
            strSql.Append("ReportDepart=@ReportDepart,");
            strSql.Append("DepartId=@DepartId,");
            strSql.Append("OperatorId=@OperatorId,");
            strSql.Append("Operator=@Operator,");
            strSql.Append("OprDepartId=@OprDepartId,");
            strSql.Append("OprDepart=@OprDepart,");
            strSql.Append("CityId=@CityId,");
            strSql.Append("CompanyId=@CompanyId,");
            strSql.Append("AddTime=@AddTime,");
            strSql.Append("Status=@Status,");
            strSql.Append("houseId=@houseId,");
            strSql.Append("houseAddress=@houseAddress,");
            strSql.Append("ViewTime=@ViewTime");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "ReportCode", DbType.AnsiString, model.ReportCode);
            db.AddInParameter(dbCommand, "CustomerId", DbType.Guid, model.CustomerId);
            db.AddInParameter(dbCommand, "CustomerName", DbType.AnsiString, model.CustomerName);
            db.AddInParameter(dbCommand, "Description", DbType.AnsiString, model.Description);
            db.AddInParameter(dbCommand, "Reportor", DbType.AnsiString, model.Reportor);
            db.AddInParameter(dbCommand, "ReportorId", DbType.Guid, model.ReportorId);
            db.AddInParameter(dbCommand, "ReportDepart", DbType.AnsiString, model.ReportDepart);
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "OperatorId", DbType.Guid, model.OperatorId);
            db.AddInParameter(dbCommand, "Operator", DbType.AnsiString, model.Operator);
            db.AddInParameter(dbCommand, "OprDepartId", DbType.Guid, model.OprDepartId);
            db.AddInParameter(dbCommand, "OprDepart", DbType.AnsiString, model.OprDepart);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, model.Status);
            db.AddInParameter(dbCommand, "houseId", DbType.Guid, model.houseId);
            db.AddInParameter(dbCommand, "houseAddress", DbType.AnsiString, model.houseAddress);
            db.AddInParameter(dbCommand, "ViewTime", DbType.DateTime, model.ViewTime);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Customer_ViewHouseInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.House.Entity.Customer_ViewHouseInfo GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,ReportCode,CustomerId,CustomerName,Description,Reportor,ReportorId,ReportDepart,DepartId,OperatorId,Operator,OprDepartId,OprDepart,CityId,CompanyId,AddTime,Status,houseId,houseAddress,ViewTime from Customer_ViewHouseInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.House.Entity.Customer_ViewHouseInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,ReportCode,CustomerId,CustomerName,Description,Reportor,ReportorId,ReportDepart,DepartId,OperatorId,Operator,OprDepartId,OprDepart,CityId,CompanyId,AddTime,Status,houseId,houseAddress,ViewTime ");
            strSql.Append(" FROM Customer_ViewHouseInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string TableName, string StrWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");



            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }


        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.House.Entity.Customer_ViewHouseInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,ReportCode,CustomerId,CustomerName,Description,Reportor,ReportorId,ReportDepart,DepartId,OperatorId,Operator,OprDepartId,OprDepart,CityId,CompanyId,AddTime,Status,houseId,houseAddress,ViewTime ");
            strSql.Append(" FROM Customer_ViewHouseInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.House.Entity.Customer_ViewHouseInfo> list = new List<Chenxin.Lib.House.Entity.Customer_ViewHouseInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.House.Entity.Customer_ViewHouseInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.House.Entity.Customer_ViewHouseInfo model = new Chenxin.Lib.House.Entity.Customer_ViewHouseInfo();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            model.ReportCode = dataReader["ReportCode"].ToString();
            ojb = dataReader["CustomerId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CustomerId = new Guid(ojb.ToString());
            }
            model.CustomerName = dataReader["CustomerName"].ToString();
            model.Description = dataReader["Description"].ToString();
            model.Reportor = dataReader["Reportor"].ToString();
            ojb = dataReader["ReportorId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ReportorId = new Guid(ojb.ToString());
            }
            model.ReportDepart = dataReader["ReportDepart"].ToString();
            ojb = dataReader["DepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DepartId = new Guid(ojb.ToString());
            }
            ojb = dataReader["OperatorId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OperatorId = new Guid(ojb.ToString());
            }
            model.Operator = dataReader["Operator"].ToString();
            ojb = dataReader["OprDepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OprDepartId = new Guid(ojb.ToString());
            }
            model.OprDepart = dataReader["OprDepart"].ToString();
            ojb = dataReader["CityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CityId = new Guid(ojb.ToString());
            }
            ojb = dataReader["CompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompanyId = new Guid(ojb.ToString());
            }
            ojb = dataReader["AddTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddTime = (DateTime)ojb;
            }
            ojb = dataReader["Status"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Status = (int)ojb;
            }
            ojb = dataReader["houseId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.houseId = new Guid(ojb.ToString());
            }
            model.houseAddress = dataReader["houseAddress"].ToString();
            ojb = dataReader["ViewTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ViewTime = (DateTime)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}

